package com.shetou.yun.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shetou.yun.R;
import com.shetou.yun.activity.BaseApplication;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Haoz on 2017/4/6 0006.
 */

/**
 * 检查更新时有新版本发布弹出对话框
 * Created by zhyh on 2015/11/16.
 */
public class NewVersionDialogFragment extends DialogFragment {
    private String mVersionCode;
    private String mDescription;
    private OnUpdateListener mOnUpdateListener;

    public void setOnUpdateListener(OnUpdateListener onUpdateListener) {
        mOnUpdateListener = onUpdateListener;
    }

    public interface OnUpdateListener {
        void onUpdate(String name, String sex, String age, String Medical_history, String tele);
    }

    public static NewVersionDialogFragment newInstance(String versionCode, String description) {
        NewVersionDialogFragment fragment = new NewVersionDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("version", versionCode);
        bundle.putString("description", description);
        fragment.setArguments(bundle);
        return fragment;
    }

    List<String> ccList;
    StringBuilder stringBuilder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        mVersionCode = bundle.getString("version");
        mDescription = bundle.getString("description");
        ccList = new ArrayList<String>();
        stringBuilder = new StringBuilder();

    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }

    String sexvalue = "男";
    EditText ed_name;
    EditText ed_age;
    EditText ed_bingshi;
    boolean xie = false;
    ImageView imageView10;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_news, null);
        ed_name = (EditText) view.findViewById(R.id.ed_name);
        ed_bingshi = (EditText) view.findViewById(R.id.ed_bingshi);
        imageView10 = (ImageView) view.findViewById(R.id.imageView10);
        imageView10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        ed_bingshi.addTextChangedListener(mTextWatcher);
        final EditText ed_tele = (EditText) view.findViewById(R.id.ed_tele);

        final TextView tv_gaoxueya = view.findViewById(R.id.tv_gaoxueya);
        final TextView tv_tangniaobing = view.findViewById(R.id.tv_tangniaobing);
        final TextView tv_shenyan = view.findViewById(R.id.tv_shenyan);
        final TextView tv_ganyan = view.findViewById(R.id.tv_ganyan);
        final TextView tv_xinjiaotong = view.findViewById(R.id.tv_xinjiaotong);
        final TextView tv_shoushu = view.findViewById(R.id.tv_shoushu);
        final TextView tv_waishang = view.findViewById(R.id.tv_waishang);
        final TextView tv_fengshixingxinzangbing = view.findViewById(R.id.tv_fengshixingxinzangbing);
        final TextView tv_changweiyan = view.findViewById(R.id.tv_changweiyan);
        final TextView tv_zhongliu = view.findViewById(R.id.tv_zhongliu);
        final TextView tv_zhiqiguanyan = view.findViewById(R.id.tv_zhiqiguanyan);
        final TextView tv_naoxueshuan = view.findViewById(R.id.tv_naoxueshuan);
        final TextView tv_nan = view.findViewById(R.id.tv_nan);
        final TextView tv_nv = view.findViewById(R.id.tv_nv);
        tv_nan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_nan.setBackground(getResources().getDrawable(R.drawable.duankuang));
                tv_nv.setBackground(null);
                sexvalue = "男";
            }
        });
        tv_nv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sexvalue = "女";
                tv_nan.setBackground(null);
                tv_nv.setBackground(getResources().getDrawable(R.drawable.duankuang));
            }
        });
        view.findViewById(R.id.tv_naoxueshuan);

        view.findViewById(R.id.layout_gaoxueya).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                stringBuilder.setLength(0);

                if (ed_bingshi.getText().toString().contains("高血压") || ed_bingshi.getText().toString().contains(",高血压")) {
                    tv_gaoxueya.setTextColor(getResources().getColor(R.color.white));
                    xie = false;
                    String newStr = ed_bingshi.getText().toString().replace("高血压", "");//得到新的字符串
                    stringBuilder.toString().replace(",高血压", "");
                    ed_bingshi.setText(newStr);
                } else {
                    xie = true;

                    tv_gaoxueya.setTextColor(getResources().getColor(R.color.gray));
                    String newStr = ed_bingshi.getText().toString().replace("高血压", ""); //得到新的字符串
                    if (!newStr.toString().contains("高血压")) {
                        stringBuilder.setLength(0);
                        stringBuilder.append(newStr);
                        stringBuilder.append(",高血压");
                    } else {

                    }
                    if (stringBuilder.toString().startsWith(",")) {
                        ed_bingshi.setText(stringBuilder.substring(1, stringBuilder.length()));

                    } else {
                        ed_bingshi.setText(stringBuilder);
                    }

                }

            }
        });
        view.findViewById(R.id.layout_tangniaobing).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ed_bingshi.getText().toString().contains("糖尿病")) {
                    tv_tangniaobing.setTextColor(getResources().getColor(R.color.white));
                    xie = false;
                    String newStr = ed_bingshi.getText().toString().replace(",糖尿病", ""); //得到新的字符串
                    stringBuilder.toString().replace(",糖尿病", "");
                    ed_bingshi.setText(newStr);
                } else {
                    xie = true;

                    tv_tangniaobing.setTextColor(getResources().getColor(R.color.gray));
                    String newStr = ed_bingshi.getText().toString().replace("糖尿病", ""); //得到新的字符串
                    if (!newStr.toString().contains("糖尿病")) {
                        stringBuilder.setLength(0);
                        stringBuilder.append(newStr);
                        stringBuilder.append(",");
                        stringBuilder.append("糖尿病");
                    } else {

                    }
                    if (stringBuilder.toString().startsWith(",")) {
                        ed_bingshi.setText(stringBuilder.substring(1, stringBuilder.length()));

                    } else {
                        ed_bingshi.setText(stringBuilder);
                    }

                }


            }
        });
        view.findViewById(R.id.layout_shenyan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (ed_bingshi.getText().toString().contains("肾炎")) {
                    tv_shenyan.setTextColor(getResources().getColor(R.color.white));
                    xie = false;
                    String newStr = ed_bingshi.getText().toString().replace(",肾炎", ""); //得到新的字符串
                    stringBuilder.toString().replace(",肾炎", "");
                    ed_bingshi.setText(newStr);
                } else {
                    xie = true;

                    tv_shenyan.setTextColor(getResources().getColor(R.color.gray));
                    String newStr = ed_bingshi.getText().toString().replace("肾炎", ""); //得到新的字符串
                    if (!newStr.toString().contains("肾炎")) {
                        stringBuilder.setLength(0);
                        stringBuilder.append(newStr);
                        stringBuilder.append(",");
                        stringBuilder.append("肾炎");
                    } else {

                    }
                    if (stringBuilder.toString().startsWith(",")) {
                        ed_bingshi.setText(stringBuilder.substring(1, stringBuilder.length()));

                    } else {
                        ed_bingshi.setText(stringBuilder);
                    }
                }

            }
        });
        view.findViewById(R.id.layout_ganyan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ed_bingshi.getText().toString().contains("肝炎")) {
                    tv_ganyan.setTextColor(getResources().getColor(R.color.white));
                    xie = false;
                    String newStr = ed_bingshi.getText().toString().replace(",肝炎", ""); //得到新的字符串
                    stringBuilder.toString().replace(",肝炎", "");
                    ed_bingshi.setText(newStr);
                } else {
                    xie = true;

                    tv_ganyan.setTextColor(getResources().getColor(R.color.gray));
                    String newStr = ed_bingshi.getText().toString().replace("肝炎", ""); //得到新的字符串
                    if (!newStr.toString().contains("肝炎")) {
                        stringBuilder.setLength(0);
                        stringBuilder.append(newStr);
                        stringBuilder.append(",");
                        stringBuilder.append("肝炎");
                    } else {

                    }
                    if (stringBuilder.toString().startsWith(",")) {
                        ed_bingshi.setText(stringBuilder.substring(1, stringBuilder.length()));

                    } else {
                        ed_bingshi.setText(stringBuilder);
                    }
                }

            }
        });
        view.findViewById(R.id.layout_xinjiaotong).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ed_bingshi.getText().toString().contains("心绞痛")) {
                    tv_xinjiaotong.setTextColor(getResources().getColor(R.color.white));
                    xie = false;
                    String newStr = ed_bingshi.getText().toString().replace(",心绞痛", ""); //得到新的字符串
                    stringBuilder.toString().replace(",心绞痛", "");
                    ed_bingshi.setText(newStr);
                } else {
                    xie = true;

                    tv_xinjiaotong.setTextColor(getResources().getColor(R.color.gray));
                    String newStr = ed_bingshi.getText().toString().replace("心绞痛", ""); //得到新的字符串
                    if (!newStr.toString().contains("心绞痛")) {
                        stringBuilder.setLength(0);
                        stringBuilder.append(newStr);
                        stringBuilder.append(",");
                        stringBuilder.append("心绞痛");
                    } else {

                    }
                    if (stringBuilder.toString().startsWith(",")) {
                        ed_bingshi.setText(stringBuilder.substring(1, stringBuilder.length()));

                    } else {
                        ed_bingshi.setText(stringBuilder);
                    }
                }


            }
        });
        view.findViewById(R.id.layout_shoushu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (ed_bingshi.getText().toString().contains("手术")) {
                    tv_shoushu.setTextColor(getResources().getColor(R.color.white));
                    xie = false;
                    String newStr = ed_bingshi.getText().toString().replace(",手术", ""); //得到新的字符串
                    stringBuilder.toString().replace(",手术", "");
                    ed_bingshi.setText(newStr);
                } else {
                    xie = true;

                    tv_shoushu.setTextColor(getResources().getColor(R.color.gray));
                    String newStr = ed_bingshi.getText().toString().replace("手术", ""); //得到新的字符串
                    if (!newStr.toString().contains("手术")) {
                        stringBuilder.setLength(0);
                        stringBuilder.append(newStr);
                        stringBuilder.append(",");
                        stringBuilder.append("手术");
                    } else {

                    }
                    if (stringBuilder.toString().startsWith(",")) {
                        ed_bingshi.setText(stringBuilder.substring(1, stringBuilder.length()));

                    } else {
                        ed_bingshi.setText(stringBuilder);
                    }
                }


            }
        });
        view.findViewById(R.id.layout_waishang).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (ed_bingshi.getText().toString().contains("外伤")) {
                    tv_waishang.setTextColor(getResources().getColor(R.color.white));
                    xie = false;
                    String newStr = ed_bingshi.getText().toString().replace(",外伤", ""); //得到新的字符串
                    stringBuilder.toString().replace(",外伤", "");
                    ed_bingshi.setText(newStr);
                } else {
                    xie = true;

                    tv_waishang.setTextColor(getResources().getColor(R.color.gray));
                    String newStr = ed_bingshi.getText().toString().replace("外伤", ""); //得到新的字符串
                    if (!newStr.toString().contains("外伤")) {
                        stringBuilder.setLength(0);
                        stringBuilder.append(newStr);
                        stringBuilder.append(",");
                        stringBuilder.append("外伤");
                    } else {

                    }
                    if (stringBuilder.toString().startsWith(",")) {
                        ed_bingshi.setText(stringBuilder.substring(1, stringBuilder.length()));

                    } else {
                        ed_bingshi.setText(stringBuilder);
                    }
                }


            }
        });
        view.findViewById(R.id.layout_fengshi).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ed_bingshi.getText().toString().contains("风湿性心脏病")) {
                    tv_fengshixingxinzangbing.setTextColor(getResources().getColor(R.color.white));
                    xie = false;
                    String newStr = ed_bingshi.getText().toString().replace(",风湿性心脏病", ""); //得到新的字符串
                    stringBuilder.toString().replace(",风湿性心脏病", "");
                    ed_bingshi.setText(newStr);
                } else {
                    xie = true;

                    tv_fengshixingxinzangbing.setTextColor(getResources().getColor(R.color.gray));
                    String newStr = ed_bingshi.getText().toString().replace("风湿性心脏病", ""); //得到新的字符串
                    if (!newStr.toString().contains("风湿性心脏病")) {
                        stringBuilder.setLength(0);
                        stringBuilder.append(newStr);
                        stringBuilder.append(",");
                        stringBuilder.append("风湿性心脏病");
                    } else {

                    }
                    if (stringBuilder.toString().startsWith(",")) {
                        ed_bingshi.setText(stringBuilder.substring(1, stringBuilder.length()));

                    } else {
                        ed_bingshi.setText(stringBuilder);
                    }
                }


            }

        });
        view.findViewById(R.id.layout_changweiyan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (ed_bingshi.getText().toString().contains("肠胃炎")) {
                    tv_changweiyan.setTextColor(getResources().getColor(R.color.white));
                    xie = false;
                    String newStr = ed_bingshi.getText().toString().replace(",肠胃炎", ""); //得到新的字符串
                    stringBuilder.toString().replace(",肠胃炎", "");
                    ed_bingshi.setText(newStr);
                } else {
                    xie = true;

                    tv_changweiyan.setTextColor(getResources().getColor(R.color.gray));
                    String newStr = ed_bingshi.getText().toString().replace("肠胃炎", ""); //得到新的字符串
                    if (!newStr.toString().contains("肠胃炎")) {
                        stringBuilder.setLength(0);
                        stringBuilder.append(newStr);
                        stringBuilder.append(",");
                        stringBuilder.append("肠胃炎");
                    } else {

                    }
                    if (stringBuilder.toString().startsWith(",")) {
                        ed_bingshi.setText(stringBuilder.substring(1, stringBuilder.length()));

                    } else {
                        ed_bingshi.setText(stringBuilder);
                    }


                }


            }
        });
        view.findViewById(R.id.layout_zhongliu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (ed_bingshi.getText().toString().contains("肿瘤")) {
                    tv_zhongliu.setTextColor(getResources().getColor(R.color.white));
                    xie = false;
                    String newStr = ed_bingshi.getText().toString().replace(",肿瘤", ""); //得到新的字符串
                    stringBuilder.toString().replace(",肿瘤", "");
                    ed_bingshi.setText(newStr);
                } else {
                    xie = true;

                    tv_zhongliu.setTextColor(getResources().getColor(R.color.gray));
                    String newStr = ed_bingshi.getText().toString().replace("肿瘤", ""); //得到新的字符串
                    if (!newStr.toString().contains("肿瘤")) {
                        stringBuilder.setLength(0);
                        stringBuilder.append(newStr);
                        stringBuilder.append(",");
                        stringBuilder.append("肿瘤");
                    } else {

                    }
                    if (stringBuilder.toString().startsWith(",")) {
                        ed_bingshi.setText(stringBuilder.substring(1, stringBuilder.length()));

                    } else {
                        ed_bingshi.setText(stringBuilder);

                    }
                }

            }
        });
        view.findViewById(R.id.layout_zhiqiguanyan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                if (ed_bingshi.getText().toString().contains("支气管炎")) {
                    tv_zhiqiguanyan.setTextColor(getResources().getColor(R.color.white));
                    xie = false;
                    String newStr = ed_bingshi.getText().toString().replace(",支气管炎", ""); //得到新的字符串
                    stringBuilder.toString().replace(",支气管炎", "");
                    ed_bingshi.setText(newStr);
                } else {
                    xie = true;

                    tv_zhiqiguanyan.setTextColor(getResources().getColor(R.color.gray));
                    String newStr = ed_bingshi.getText().toString().replace("支气管炎", ""); //得到新的字符串
                    if (!newStr.toString().contains("支气管炎")) {
                        stringBuilder.setLength(0);
                        stringBuilder.append(newStr);
                        stringBuilder.append(",");
                        stringBuilder.append("支气管炎");
                    } else {

                    }
                    if (stringBuilder.toString().startsWith(",")) {
                        ed_bingshi.setText(stringBuilder.substring(1, stringBuilder.length()));

                    } else {
                        ed_bingshi.setText(stringBuilder);

                    }
                }


            }
        });
        view.findViewById(R.id.layout_naoxueshuan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                if (ed_bingshi.getText().toString().contains("脑血栓")) {
                    tv_naoxueshuan.setTextColor(getResources().getColor(R.color.white));
                    xie = false;
                    String newStr = ed_bingshi.getText().toString().replace(",脑血栓", ""); //得到新的字符串
                    stringBuilder.toString().replace(",脑血栓", "");
                    ed_bingshi.setText(newStr);
                } else {
                    xie = true;

                    tv_naoxueshuan.setTextColor(getResources().getColor(R.color.gray));
                    String newStr = ed_bingshi.getText().toString().replace("脑血栓", ""); //得到新的字符串
                    if (!newStr.toString().contains("脑血栓")) {
                        stringBuilder.setLength(0);
                        stringBuilder.append(newStr);
                        stringBuilder.append(",");
                        stringBuilder.append("脑血栓");
                    } else {

                    }
                    if (stringBuilder.toString().startsWith(",")) {
                        ed_bingshi.setText(stringBuilder.substring(1, stringBuilder.length()));

                    } else {
                        ed_bingshi.setText(stringBuilder);

                    }
                }


            }
        });


        ed_age = (EditText) view.findViewById(R.id.ed_age);

        Button button = (Button) view.findViewById(R.id.submit_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnUpdateListener != null) {
                    String namevalue = ed_name.getText().toString();
                    if (TextUtils.isEmpty(namevalue)) {
                        Toast.makeText(BaseApplication.getInstance(), "请输入姓名！", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    String agevalue = ed_age.getText().toString();
                    String televalue = ed_tele.getText().toString();

                    if (TextUtils.isEmpty(agevalue)) {
                        Toast.makeText(BaseApplication.getInstance(), "请输入年龄!", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (TextUtils.isEmpty(ed_bingshi.getText().toString())) {
                        ed_bingshi.setText("无");

                    }

                    if (TextUtils.isEmpty(televalue)) {
                        Toast.makeText(BaseApplication.getInstance(), "请输入电话号码!", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    int length = televalue.length();

                    if (length > 7 && length <= 11) {

                    } else {
                        Toast.makeText(BaseApplication.getInstance(), "请输入正确电话号码!", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    if (stringBuilder.toString().startsWith(",")) {
                        mOnUpdateListener.onUpdate(namevalue, sexvalue, agevalue, stringBuilder.substring(1, stringBuilder.length()), televalue);


                    } else {
                        mOnUpdateListener.onUpdate(namevalue, sexvalue, agevalue, stringBuilder.toString(), televalue);
                    }

                }
            }
        });
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.75), (int) (dm.heightPixels * 0.75));
        }
    }

    public void removeone(List list, String ST) {
        for (int i = 0; i < list.size(); i++) {

            if (list.get(i).equals(ST)) {

                list.remove(i);

            }

        }
    }

    TextWatcher mTextWatcher = new TextWatcher() {
        private CharSequence temp;
        private int editStart;
        private int editEnd;

        @Override
        public void beforeTextChanged(CharSequence s, int arg1, int arg2,
                                      int arg3) {
//            Log.e("7788", " s" + s);
//            temp = s;
        }

        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2,
                                  int arg3) {


//            mTextView.setText(s);
//            Log.e("2", " s1:" + s);
//            Log.e("7788", " s2:" + arg1);
//            Log.e("7788", " s3:" + arg2);
//            Log.e("7788", " s4:" + arg3);
//            "肾炎" || "风湿性心脏病" || "心绞痛" || "糖尿病" || "手术" || "肿瘤" || "高血压" || "肝炎" || "外伤" || "支气管炎" || "肠胃炎" || "脑血栓"
//            if (!s.toString().contains("肾炎"
//            )) {
//                Log.e("7788", " 不包含" + 111);
//            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.e("7788", " s2" + s);
//            if (xie) {
//                ed_bingshi.setText(s.toString());
//            } else {
//
//            }
//            editStart = ed_bingshi.getSelectionStart();
//            editEnd = ed_bingshi.getSelectionEnd();
//            if (temp.length() > 10) {
////                Toast.makeText(TextWatcherDemo.this,
////                        "你输入的字数已经超过了限制！", Toast.LENGTH_SHORT)
////                        .show();
//                s.delete(editStart-1, editEnd);
//                int tempSelection = editStart;
//                mEditText.setText(s);
//                mEditText.setSelection(tempSelection);
//            }
        }
    };

}


